//
//  Particle.m
//  Void Dead
//
//  Created by Sidney Just on 12.09.09.
//  Copyright 2009 Sidney Just
//  This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; 
//  either version 3 of the License, or (at your option) any later version.
//  
//  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
//  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//  

#import "Particle.h"
#import "ParticleEmitter.h"
#import "panel.h"
#import "Engine.h"

@implementation VD_Particle
@synthesize panel, pos_x, pos_y, gain_X, gain_Y, lifespan, randomX, randomY, gravityX, gravityY, gainSize_x, gainSize_y;

- (id)initWithEngine:(VD_Engine *)engine andLayer:(int)layer andTexture:(int)texture {
	if(self = [super init])
	{
		ccEngine = engine;
		
		panel = [ccEngine->panManager createPanel:layer];
		panel.texture = texture;
		[ccEngine->panManager createDefaultShape:panel];
		
		tPos_x = 0;
		tPos_y = 0;
		
		currForceX = 0;
		currForceX = 0;
	}
	return self;
}

- (void)dealloc {
	[ccEngine->panManager deletePanel:panel];
	[super dealloc];
}

- (void)setDirection {
	if(randomX > 0)
	{
		tPos_x = (float)(((int)(randomX))-arc4random()%((int)(randomX*2)));
		tPos_x -= (float)(randomX/2);
	}
	
	if(randomY > 0)
	{
		tPos_y = (float)(((int)(randomY))-arc4random()%((int)(randomY*2)));
		tPos_y -= (float)(randomY/2);
	}
}

- (void)updateMe {
	currForceX += gravityX * ccEngine->timeStep;
	currForceY += gravityY * ccEngine->timeStep;
	
	pos_x += (tPos_x + gain_X + currForceX) * ccEngine->timeStep;
	pos_y += (tPos_y + gain_Y + currForceY) * ccEngine->timeStep;
	
	lifespan -= ccEngine->timeStep;
	
	panel.pos_x = pos_x;
	panel.pos_y = pos_y;
	
	panel.size.x += (gainSize_x * ccEngine->timeStep);
	panel.size.y += (gainSize_y * ccEngine->timeStep);
}

@end
